<?php

namespace CarApi\Controller;
use Think\Controller;
//use Common\RestController;
use Common\CommonCarController;
use Common\Model\CarTurnoverModel;
use Common\Model\PatientModel;
use Common\Model\PatientLifeStatusModel;
use Common\Model\PatientNormalCureModel;
use Common\Model\PatientMedicineCureModel;
use Common\Model\CaseLogicModel;

class PatientController extends CommonCarController {


	private $carTurnoverModel;
	private $patientModel;

	public function __construct(){
		parent::__construct();

		$this->carTurnoverModel = new CarTurnoverModel;
		$this->patientModel = new PatientModel;
	}

	/**
	 * 获取病人信息
	 * @type post
	 * @param token, patient_id
	 */
	public function getPatientInfo(){
		$patient_id = I('patient_id');
		if( empty($patient_id) ){
			$this->ajaxReturn(['status'=>0,'error_info'=>'病人id必须填写']);
		}

		$info = $this->patientModel->getPatientInfo(I('patient_id'));
		if(empty($info)){
			$this->ajaxReturn(['status'=>0,'error_info'=>'该病人不存在']);
		}

		//返回病人信息
		$this->ajaxReturn(['status'=>1,'data'=>$info]);
	}

	/**
	 * 添加病人
	 * car_id
	 * name,sex,phone,incident_location
	 */
	public function addPatient(){
		$carId = I('car_id');
		if(empty($carId)){
			//没有车量号，出新车
			$this->carTurnoverModel->user120_id = $this->userCar->id;
			$carId = $this->carTurnoverModel->add();
		}

		if(!$this->patientModel->create()){
			$this->carTurnoverModel->where(['id'=>$carId])->delete();
			$this->ajaxReturn(['status'=>0,'error_info'=>$this->patientModel->getError()]);
		}
		$this->patientModel->car_id = $carId;
		$this->patientModel->user120_id = $this->userCar->id;
		$patient_id = $this->patientModel->add();
		$this->ajaxReturn(['status'=>1,'data'=>['car_id'=>$carId,'patient_id'=>$patient_id]]);
	}

	/**
	 * 修改病人信息
	 * patient_id,
	 * patient_info
	 */
	public function editPatient(){
		$patient_id = I('patient_id');
		if(empty($patient_id)){
			$this->ajaxReturn(['status'=>0,'error_info'=>'接口传参错误，需要传patient_id']);
		}

		$data = $_POST;

		if(!$this->patientModel->create()){
			$this->ajaxReturn(['status'=>0,'error_info'=>$this->patientModel->getError()]);
		}

		$this->patientModel->where(['id'=>$patient_id])->save();
		$this->ajaxReturn(['status'=>1]);
	}

	/**
	 * 添加生命体征信息
	 * patient_id,heart_rate,blood_pressure,oxygen_saturation,breathing_rate,record_time 当时记录的时间戳
	 */
	public function addLifeStatus(){
		$lifeModel = new PatientLifeStatusModel;
		if(!$lifeModel->create()){
			$this->ajaxReturn(['status'=>0,'error_info'=>$lifeModel->getError()]);
		}

		$lifeModel->add();
		$this->ajaxReturn(['status'=>1]);
	}

	/**
	 * 添加一般治疗信息
	 * token
	 * car_id
	 * patient_id
	 * method_id 对应表中有数据 "[1,2,3,4,5]"
	 */
	public function addNormalCure(){
		$patientNormalCureModel = new patientNormalCureModel;
		$res = $patientNormalCureModel->addNormalCure(I('car_id'),I('patient_id'),I('method_id'));
		$this->ajaxReturn($res);
	}

	/**
	 * 添加药品治疗信息
	 * token
	 * car_id
	 * patient_id
	 * medicine_list = [
	 	{"medicine_id":"21","format":"药品规格","unit":"药品单位","num":"药品数量","amount":"药品用量","method":"途径"},{}
	 ]
	 */
	public function addMedicineCure(){
		$patientMedicineCure = new PatientMedicineCureModel;
		$res = $patientMedicineCure->addMedicineCure(I('car_id'),I('patient_id'),I('medicine_list'));
		$this->ajaxReturn($res);
	}

	/**
	 * 120端提交交接信息
	 * token 
	 * car_id ,patient_id
	 * signature 120用户签名
	 */
	public function closeCase(){
		$caseLogicModel = new CaseLogicModel;
		$result = $caseLogicModel->closeCase(I('car_id'),I('patient_id'),I('signature'),$this->userCar->id);
		$this->ajaxReturn($result);
	}
    
}